//
//  HXDShopInfoTableViewController.m
//  qingDaoCircleOfLife
//
//  Created by huxuedong on 15/11/28.
//  Copyright © 2015年 huxuedong. All rights reserved.
//

#import "HXDShopInfoTableViewController.h"
#import "HXDHTTPSessionManager.h"
#import "HXDShopModel.h"
#import "HXDGoodsModel.h"
#import "HXDCommentModel.h"
#import "HXDHeaderView.h"
#import "HXDGoodsTableViewCell.h"
#import "HXDEvaluationTableViewCell.h"
#import "HXDPublishTableViewCell.h"
#import "UMSocial.h"
#import "HXDGoods.h"
#import "HXDComment.h"

@interface HXDShopInfoTableViewController () <UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *shopList;
@property (strong, nonatomic) NSMutableArray *goodsList;
@property (strong, nonatomic) NSMutableArray *commentList;
@property (strong, nonatomic) NSNumber *score;

@end

@implementation HXDShopInfoTableViewController

static NSString *ID = @"tabelViewCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titleLabel.text = @"商家详情";
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shareAction) name:@"share" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sendAction:) name:@"send" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orLogin:) name:@"isLogin" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(star:) name:@"score" object:nil];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"shop_info.plist" ofType:nil];
    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
    HXDShopModel *tempShop = [[HXDShopModel alloc] initWithDictionary:dict error:nil];
    HXDGoodsModel *tempGoods = [[HXDGoodsModel alloc] initWithDictionary:dict error:nil];
    HXDCommentModel *tempComment = [[HXDCommentModel alloc] initWithDictionary:dict error:nil];
    NSMutableArray *shopList = tempShop.shop.list;
    NSMutableArray *goodsList = tempGoods.goods.list;
    NSMutableArray *commentList = tempComment.comment.list;
    self.shopList = shopList;
    self.goodsList = goodsList;
    self.commentList = commentList;
}

- (void)star:(NSNotification *)notification {
    self.score = notification.object;
}

- (void)orLogin:(NSNotification *)notification {
    if (notification.object) {
        self.rightButton.frame = CGRectMake(kUIScreenWidth - 2 * kNavigationButtonWidth + 30, kUIStatusBarHeight, kNavigationButtonWidth, kNavigationButtonWidth);
        [self.rightButton setTitle:nil forState:UIControlStateNormal];
        UIImage *icon = [UIImage imageWithData:[kSaveData objectForKey:kIconImage]];
        [self.rightButton setBackgroundImage:icon forState:UIControlStateNormal];
        self.rightButton.layer.cornerRadius = 20;
        self.rightButton.clipsToBounds = YES;
        self.rightButton.userInteractionEnabled = NO;
    }
}

- (void)backMethod {
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0 || section == 3) {
        return 1;
    } else if (section == 1) {
        return self.goodsList.count;
    } else {
        return self.commentList.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        }
        HXDHeaderView *headerView = [HXDHeaderView headerView];
        headerView.shopModel = self.shopList[0];
        [cell addSubview:headerView];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else if (indexPath.section == 1) {
        HXDGoodsTableViewCell *cell = [HXDGoodsTableViewCell goodsTableViewCell];
        cell.goods = self.goodsList[indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else if (indexPath.section == 2) {
        HXDEvaluationTableViewCell *cell = [HXDEvaluationTableViewCell evaluationTableViewCell];
        cell.comment = self.commentList[indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else {
        HXDPublishTableViewCell *cell = [HXDPublishTableViewCell publishTableViewCell];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 250;
    } else if (indexPath.section == 1) {
        return 80;
    } else {
        return 150;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 1) {
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.image = [UIImage imageNamed:@"shoper"];
        return imageView;
    } else if (section == 2) {
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.image = [UIImage imageNamed:@"ping"];
        return imageView;
    } else {
        return nil;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 1 || section == 2) {
        return 25;
    } else {
       return 0;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section == 1) {
        UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, 45)];
        UIButton *footer = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, 25)];
        [footer setTitle:@"更多" forState:0];
        footer.titleLabel.font = [UIFont systemFontOfSize:15];
        [footer setImage:[UIImage imageNamed:@"business_more"] forState:0];
        [footer setTitleColor:[UIColor colorWithRed:70 / 255.0 green:130 / 255.0 blue:250 / 255.0 alpha:1] forState:0];
        [footer setBackgroundColor:[UIColor whiteColor]];
        [footer setTitleEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
        [footer addTarget:self action:@selector(addGoodsCell) forControlEvents:UIControlEventTouchUpInside];
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 25, kUIScreenWidth, 20)];
        view.backgroundColor = [UIColor colorWithRed:238 / 255.0 green:238 / 255.0 blue:244 / 255.0 alpha:1];
        [footerView addSubview:footer];
        [footerView addSubview:view];
        return footerView;
    } else if (section == 2) {
        UIButton *footer = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, 25)];
        [footer setTitle:@"查看更多评论" forState:0];
        footer.titleLabel.font = [UIFont systemFontOfSize:15];
        [footer setImage:[UIImage imageNamed:@"business_more"] forState:0];
        [footer setTitleColor:[UIColor colorWithRed:70 / 255.0 green:130 / 255.0 blue:250 / 255.0 alpha:1] forState:0];
        [footer setBackgroundColor:[UIColor whiteColor]];
        [footer setTitleEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
        [footer addTarget:self action:@selector(addEvaluationCell) forControlEvents:UIControlEventTouchUpInside];
        return footer;
    } else {
        return nil;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 1) {
        return 45;
    } else if (section == 2) {
        return 25;
    } else {
        return 0;
    }
}

- (void)addGoodsCell {
    HXDGoods *goods = [[HXDGoods alloc] init];
    goods.title = @"炸鸡+啤酒";
    goods.price = @"69";
    goods.intro = @"看韩剧，炸鸡和啤酒更配哦！";
    [self.goodsList addObject:goods];
    [self.tableView reloadData];
}

- (void)addEvaluationCell {
    HXDComment *comment = [[HXDComment alloc] init];
    comment.name = @"冀东";
    comment.photo = @"http://f.hiphotos.baidu.com/image/pic/item/b3b7d0a20cf431ada66989d44d36acaf2fdd98a0.jpg";
    comment.time = @"2015-11-30";
    comment.score = @"5";
    comment.comment = @"太开心了，我望眼欲穿的榴莲蛋糕啊，远远就闻到了香香的味道，切开后可以看到满满的榴莲，咬着丝丝绵绵的果肉，品着咖啡，绝配啊！下次还要来这里，尝一尝其它口味，据说个个都是经典火爆哦*_*";
    [self.commentList addObject:comment];
    [self.tableView reloadData];
}

- (void)sendAction:(NSNotification *)notification {
    NSString *publishText = notification.object;
    HXDComment *comment = [[HXDComment alloc] init];
    comment.name = @"小白";
    comment.photo = @"http://f.hiphotos.baidu.com/zhidao/wh%3D600%2C800/sign=4aecaa260855b3199cac8a737399ae1e/241f95cad1c8a7862c38ac296509c93d70cf5083.jpg";
    NSDate *date = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd";
    comment.time = [formatter stringFromDate:date];
    comment.score = [NSString stringWithFormat:@"%d",self.score.intValue];
    comment.comment = publishText;
    [self.commentList addObject:comment];
    [self.tableView reloadData];
}

- (void)keyboardDidChangeFrame:(NSNotification *)notification {
    CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat currentY = keyboardFrame.origin.y;
    CGFloat transformSide = currentY - [UIScreen mainScreen].bounds.size.height;
    self.view.transform = CGAffineTransformMakeTranslation(0, transformSide);
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:3];
    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

- (void)shareAction {
    //友盟分享
    [UMSocialSnsService presentSnsIconSheetView:self appKey:@"5657328767e58ea562003340"
                                      shareText:@"大耳朵图图"
                                     shareImage:[UIImage imageNamed:@"tutu"]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToWechatSession,UMShareToQQ, UMShareToDouban, UMShareToEmail, UMShareToFacebook,nil]
                                       delegate:nil];
}

@end
